package com.dlc.shop.delivery.platform.controller;

import cn.hutool.core.util.PhoneUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.bean.enums.OfflineHandleEventType;
import com.dlc.shop.bean.enums.StationEnum;
import com.dlc.shop.bean.model.OfflineHandleEvent;
import com.dlc.shop.bean.model.Station;
import com.dlc.shop.bean.param.OfflineHandleEventAuditParam;
import com.dlc.shop.common.enums.SysTypeEnum;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.security.common.bo.UidInfoBO;
import com.dlc.shop.security.common.manager.TokenStore;
import com.dlc.shop.security.platform.util.SecurityUtils;
import com.dlc.shop.service.OfflineHandleEventService;
import com.dlc.shop.service.StationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author YXF
 * @date 2020-04-23 15:18:29
 */
@RestController
@RequestMapping("/platform/station")
@Tag(name = "平台端自提接口")
@AllArgsConstructor
public class StationController {

    private final StationService stationService;
    private final OfflineHandleEventService offlineHandleEventService;
    private final TokenStore tokenStore;


    @GetMapping("/page")
    @Operation(summary = "分页查询自提点信息")
    @PreAuthorize("@pms.hasPermission('platform:station:page')")
    public ServerResponseEntity<IPage<Station>> getStationPage(PageParam<Station> page, Station station) {
        return ServerResponseEntity.success(stationService.getPage(page, station));
    }

    @GetMapping("/info/{stationId}")
    @Operation(summary = "通过id查询自提点")
    @PreAuthorize("@pms.hasPermission('platform:station:info')")
    @Parameter(name = "stationId", description = "自提点id" , required = true)
    public ServerResponseEntity<Station> getById(@PathVariable("stationId") Long stationId) {
        Station station = stationService.getStationInfoById(stationId);
        if (Objects.nonNull(station.getPhone())) {
            station.setPhone(PhoneUtil.hideBetween(station.getPhone()).toString());
        }
        return ServerResponseEntity.success(station);
    }

    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('platform:station:update')")
    @Operation(summary = "下线自提点")
    public ServerResponseEntity<Void> offline(@RequestBody OfflineHandleEvent offlineHandleEvent) {
        Station station = stationService.getById(offlineHandleEvent.getHandleId());
        if (station == null) {
            // 未找到该自提点信息
            throw new YamiShopBindException("yami.station.no.find");
        }
        if (Objects.equals(station.getStatus(), StationEnum.OFFLINE.getValue())) {
            // 该自提点已下线
            throw new YamiShopBindException("yami.station.was.offline");
        }
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        stationService.offline(station, offlineHandleEvent.getOfflineReason(), sysUserId);
        // 移除缓存
        stationService.removeCacheByStationId(station.getStationId());
        tokenStore.deleteTokenByUidInfo(new UidInfoBO(SysTypeEnum.STATION, station.getStationId().toString(), station.getShopId(), 0));
        return ServerResponseEntity.success();
    }

    @GetMapping("/getOfflineHandleEventByStationId/{stationId}")
    @Operation(summary = "通过自提点id获取下线信息")
    @Parameter(name = "stationId", description = "自提点id" , required = true)
    @PreAuthorize("@pms.hasPermission('platform:station:offlineInfo')")
    public ServerResponseEntity<OfflineHandleEvent> getOfflineHandleEventByDiscountId(@PathVariable("stationId") Long stationId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.STATION.getValue(), stationId);
        return ServerResponseEntity.success(offlineHandleEvent);
    }

    @PostMapping("/auditStation")
    @PreAuthorize("@pms.hasPermission('platform:station:update')")
    @Operation(summary = "审核活动")
    public ServerResponseEntity<Void> auditSeckill(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Station station = stationService.getById(offlineHandleEventAuditParam.getHandleId());
        if (station == null) {
            // 未找到该自提点信息
            throw new YamiShopBindException("yami.station.no.find");
        }
        stationService.auditSeckill(offlineHandleEventAuditParam, sysUserId);

        // 移除缓存
        stationService.removeCacheByStationId(station.getStationId());
        return ServerResponseEntity.success();
    }
}
